
package wsepForum.network.server;

import java.net.Socket;

import wsepForum.domain.Session;
import wsepForum.network.common.ConsoleToken;
import wsepForum.network.common.ConsoleTokenizer;
import wsepForum.network.server.gate.IUIController;

/**
 * @author Oren Ben-Haim
 *
 */
public class ConsoleConnectionHandler extends
		ConnectionHandler<ConsoleToken> {

	/*****************************
	 * MEMBERS                   *
	 *****************************/
	/**
	 * indicates whether or not to send back the client what he types in the console
	 */
	private final boolean ECHO = false;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public ConsoleConnectionHandler(Socket acceptedSocket,
			Session connectionSession,
			IUIController<ConsoleToken> uiController) {
		super(acceptedSocket, connectionSession, uiController);
		this.tokenizer = new ConsoleTokenizer();
		this.delimiter = "\r\n";
	}

	/**
	 * Turns char array to a string
	 * also echo the chars if ECHO is on
	 * @param buffer char buffer array to turn from
	 * @param size size of real array
	 * @return string got from the char array
	 */
	protected String getString(char[] buffer, int size) {
		if (ECHO) {
			try
			{
				for (int i=0; i<size;i++){
					this.out.write(buffer[i]);
				}
				this.out.flush();
			}
			catch (Exception e)
			{}
		}
		return new String(buffer, 0, size);
	}

}
